#!/bin/sh
# This regression test is a part of SIPp.
# With -t tn, a call's TCP connection is closed when the call ends, so
# the UAC holds about as many connections as it has calls, not one per
# call made so far.
. "`dirname "$0"`/../functions"; init

command -v ss >/dev/null || skip "no ss"

established() {
    ss -tnH state established '( dport = :25300 )' | wc -l
}

sippbg -sn uas -t tn -i 127.0.0.1 -p 25300
sippbg -sn uac -t tn -i 127.0.0.1 -p 25301 127.0.0.1:25300 \
    -r 20 -d 500 -m 60 -timeout 10 -timeout_error
uac=$pid
sleep 1
conns=`established`
wait $uac
status=$?

if test $status -ne 0; then
    fail "UAC failed with $status"
elif test $conns -gt 15; then
    fail "$conns connections for about 10 calls"
else
    ok
fi
